package luanvan.app.foodnote;

import luanvan.app.foodnote.animation.ActivitySwitcher;
import luanvan.app.foodnote.library.ConnectionDetector;
import luanvan.app.foodnote.library.SessionManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	Button btnPlaces;
	Button btnSearch;
	Button btnReport;
	Button btnHelp;
	Button btnAbout;

	// Session Manager class
	SessionManager sessionManager;
	
	// flag for Internet connection status
    Boolean isInternetPresent = false;
 
    // Connection detector class
    ConnectionDetector cd;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		sessionManager = new SessionManager(getApplicationContext());
		
		sessionManager.checkLogin();
		
		setContentView(R.layout.activity_main);
		
		btnPlaces = (Button) findViewById(R.id.btnPlaces);
		btnHelp = (Button) findViewById(R.id.btnHelp);
		btnAbout = (Button) findViewById(R.id.btnAbout);
		
		cd = new ConnectionDetector(getApplicationContext());
		isInternetPresent = cd.isConnectingToInternet();
		
		
		OnClickListener places = new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent a = new Intent(MainActivity.this, AllFoodsActivity.class);
				startActivity(a);
			}
		};
		
		OnClickListener userGuide = new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent b = new Intent(MainActivity.this, ViewPagerStyleActivity.class);
				startActivity(b);
			}
		};
		OnClickListener about = new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent b = new Intent(MainActivity.this, AboutFoodActivity.class);
				startActivity(b);
			}
		};
		btnPlaces.setOnClickListener(places);
		btnHelp.setOnClickListener(userGuide);
		btnAbout.setOnClickListener(about);
	}
	@Override
	protected void onResume(){
		ActivitySwitcher.animationIn(findViewById(R.id.container), getWindowManager());
		super.onResume();
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		//userFunction.logoutUser(getApplicationContext());
	}
	@Override
	public void onBackPressed() {
		//super.onBackPressed();
		alertDialog();
		return;
	}
	 
	 @SuppressWarnings("deprecation")
	 private void alertDialog(){
		 final AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
		 // Title dialog
		 //alert.setTitle("");
		 
		 // Message dialog
		 alert.setMessage("Bạn có muốn thoát ứng dụng?");
		 
		 alert.setIcon(R.drawable.success);
		 
		 alert.setButton("Đồng ý", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				sessionManager.logoutUser();
				finish();
			}
		});
		 alert.setButton2("Hủy", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				alert.dismiss();
			}
		});
		 
		 alert.show();
		 
	 }
}
